package com.longrise.bymodule.page.customermanage;

import android.webkit.JavascriptInterface;

import com.longrise.bymodule.R;
import com.longrise.common.base.RxManager;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.permission.PermissionUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import static com.longrise.common.utils.AppUtil.getString;

/**
 * Created by Administrator on 2017/11/28.
 * 客户管理的webBridge
 */

class ClientManagerWebBridge extends BaseWebBridge {

    public ClientManagerWebBridge(BaseWebActivity activity, RxManager rxManager) {
        super(activity, rxManager);
    }

    /**
     * 获取联系人
     */
    @JavascriptInterface
    public void getContact() {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("1111");
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        toGetContact();
                    }
                });
        mRxManager.addDisposable(subscribe);
    }


    /**
     * 获取手机联系人的权限
     */
    private void toGetContact() {
        PermissionUtil.getInstance().requestPermission(mActivity,
                PermissionUtil.Permission.CONTACTS,
                getString(R.string.contact_permission),
                getString(R.string.contact_permission_hint),
                new PermissionUtil.PermissionCallBack() {
            @Override
            public void onSuccess() {
                ((ClientManageActivity2)mActivity).startContactActivity();
            }

            @Override
            public void onDenied() {

            }
        });
    }
}
